package edu.gatech.doi.spamframework.core;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Replace {

	public void replace(String filename,String filename1,String str1,String str2)
	{
		
		FileReader fr = null;
		File file=new File(filename1);
		if(!file.exists())
		{
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			file.delete();
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			fr = new FileReader(filename);
			BufferedReader br=new BufferedReader(fr);
			FileWriter fw = new FileWriter(filename1);
			BufferedWriter bw=new BufferedWriter(fw);
			String line;
			while((line=br.readLine())!=null)
			{
				
				int index=line.indexOf(str1);
				System.out.println(index);
				StringBuffer sb=new StringBuffer();
				if(index>=0)
				{
					String substr1=line.substring(0, index);
					String substr2=line.substring(index+str1.length());
					System.out.println(substr1+" "+substr2);
					sb.append(substr1);
					sb.append(str2);
					sb.append(substr2);
					bw.append(sb);
					bw.newLine();
					bw.flush();
					
					
				}
				
			
				
			}
			br.close();
			bw.close();
			fw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
	}
	
	public void creatFile(String filename,String filename1,String str1,String str2,String type)
	{
		
		
		FileReader fr = null;
		File file=new File(filename1);
		if(!file.exists())
		{
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			file.delete();
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			fr = new FileReader(filename);
			BufferedReader br=new BufferedReader(fr);
			FileWriter fw = new FileWriter(filename1);
			BufferedWriter bw=new BufferedWriter(fw);
			String line;
			while((line=br.readLine())!=null)
			{
				if(line.contains(type))
				{
				int index=line.indexOf(str1);
				System.out.println(index);
				StringBuffer sb=new StringBuffer();
				 if(index>=0)
				 {
					String substr1=line.substring(0, index);
					String substr2=line.substring(index+str1.length());
					System.out.println(substr1+" "+substr2);
//					sb.append(substr1);
					sb.append(str2);
					sb.append(substr2);
					bw.append(sb);
					bw.newLine();
					bw.flush();
					
					
				 }
				}
			
				
			}
			br.close();
			bw.close();
			fw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	
	public static void main(String [] arg0)
	{
		Replace re=new Replace();
		String dir=System.getProperties().getProperty("user.dir")+System.getProperties().getProperty("file.separator")+"datasets"+System.getProperties().getProperty("file.separator")+"trec"+System.getProperties().getProperty("file.separator")+"2008"+System.getProperties().getProperty("file.separator");
//		String filename=dir+"spam_files";
//		String filename1=dir+"spam_files1";
		
		
//		String str1="/mnt/new_disk";
//		String str2="/disl/yavin1";
		
		String filename=dir+"index";
		String filename1=dir+"hamfile";
		String str1="../";
		String str2="/disl/yavin1/projects/spam/corpora/trec/2008/ceas08-1/";
		
//		re.replace(filename, filename1, str1, str2);
		String type="ham";
		re.creatFile(filename, filename1, str1, str2, type);
		
	}
}
